package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    //菜品分页查询
    @Override                                 //传入默认值
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构件分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //传入条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);

        page = setmealMapper.selectPage(page, qw);
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);

            setmeal.setCategoryName(category.getName());

//            // 3. 根据id查询并封装菜品列表
//            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<SetmealDish>();
//            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
//            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
//            setmeal.setSetmealDishes(setmealDishList);
        }


        return page;
    }

    //菜品
    @Override
    public List<Dish> list(Long categoryId, String name) {
//构建条件
        LambdaQueryWrapper<Dish> qw1 = new LambdaQueryWrapper<>();

        qw1.eq(categoryId != null, Dish::getCategoryId, categoryId);

        qw1.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        //按条件查询
        List<Dish> dishList = dishMapper.selectList(qw1);


        return dishList;
    }

    //新增套餐
    @Override
    public void save(Setmeal setmeal) {

        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();


        for (SetmealDish dish : setmealDishes) {
            System.out.println("gggg" + setmeal);
            //两个表建立链接
            dish.setSetmealId(setmeal.getId());
            //存入 2-2 保存数据库  insert into dish_flavor() values();
            setmealDishMapper.insert(dish);


        }

    }

    //修改
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        //获取原有口味
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        //删除原有的
        setmealDishMapper.delete(qw);
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            //口味管关联
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);


        }


    }

    //查询回显
    @Override
    public Setmeal setmealFindByid(long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        setmeal.setSetmealDishes(setmealDishes);

        return setmeal;
    }

    @Override
    public void statuSetmeal(Integer status, List<Long> ids) {
        //判断他是否为空
        if (CollUtil.isNotEmpty(ids)) {
            for (Long setmeal : ids) {
                Setmeal setmeal1 = new Setmeal();
                //修改套餐状态
                setmeal1.setStatus(status);
                //修改需要修改的id
                setmeal1.setId(setmeal);
                setmealMapper.updateById(setmeal1);
            }

//            //构建条件 update setmeal set status=1 where id in(1,2,3)
//            LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
//            qw.in(Setmeal::getId,ids); // where id in(1,2,3)
//
//            //构建实体类
//            Setmeal setmeal = new Setmeal();
//            setmeal.setStatus(status);
//
//            //修改状态
//            setmealMapper.update(setmeal,qw);

        }


    }

    //删除
    @Override
    public void deleByid(List<Long> ids) {


        //先查询 id符合 而且状态是起售的id
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus, 1);//获得状态为1的
        qw.in(Setmeal::getId, ids);//符合要求的

        //算一下有没有状态是起售的  没有是起售就执行删除
        Integer setmealCount = setmealMapper.selectCount(qw);
        if (setmealCount > 0) {
            throw new CustomException("有起售的套餐 删除删除失败");
        }

        //集合里没有是起售的就执行删除
        setmealMapper.deleteBatchIds(ids);

        // 4.删除菜品对应口味  delete from dish_flavor where dish_id in (1632199055591378946,1413384757047271425)
        LambdaQueryWrapper<SetmealDish> setmealQw = new LambdaQueryWrapper<>();
        setmealQw.in(SetmealDish::getSetmealId, ids);  // where dish_id in (1632199055591378946,1413384757047271425)
        setmealDishMapper.delete(setmealQw);
    }

    @Override
    public List<Setmeal> findAll(Long categoryId, Integer status) {
        // 1.构建条件 select * from setmeal where category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, status);

        // 2.执行查询
        return setmealMapper.selectList(qw);
    }

    @Override
    public List<Dish> selectById(Long id) {
        List<Dish> dishList = new ArrayList<>();
        Setmeal setmeal = setmealMapper.selectById(id);
        // 按setmeal id 查询 setmealdish表
        //select * from setmealdish  where   SetmealId = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);


        for (SetmealDish setmealDish : setmealDishes) {

            LambdaQueryWrapper<Dish> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(Dish::getId, setmealDish.getDishId());

            Dish dish = dishMapper.selectOne(qw1);

//            Dish dish = dishMapper.selectById(setmealDish.getDishId());

            dish.setCopies(setmealDish.getCopies());
//
            dishList.add(dish);


        }


        return dishList;
    }

}

